package cn.edu.nuc.onlinestore.frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.onlinestore.network.server.TCPClient;
import cn.edu.nuc.onlinestore.vo.Massage;
import cn.edu.nuc.onlinestore.vo.UserDo;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.ImageIcon;

public class UserLogin extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6838951146807098147L;
	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	private JFrame jframe;
	private static TCPClient tcp=null;
	private String result;
	private String massage;
	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserLogin frame = new UserLogin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 * @param dk 
	 * @param ip 
	 */
	public UserLogin(String ip, int dk) {
		tcp=new TCPClient(ip,dk);
		this.jframe=this;
		setTitle("中北线在商场-登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 461, 349);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("用户名:");
		label.setBounds(87, 146, 54, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("密  码:");
		label_1.setBounds(87, 185, 54, 15);
		contentPane.add(label_1);
		
		textField = new JTextField();
		textField.setBounds(151, 146, 197, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(151, 185, 197, 21);
		contentPane.add(passwordField);
		
		JButton button = new JButton("登录系统");
		button.setBounds(255, 216, 93, 23);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//判断登录成功与否
				StringBuilder userpass=new StringBuilder();
				String userpassword=userpass.append(passwordField.getPassword()).toString();
				//System.out.println(adminname.getText()+userpass.append(userpassword.getPassword()));//测试
				if(textField.getText().equals("")||userpassword.equals("")){
					if(textField.getText().equals("")){
						JOptionPane.showMessageDialog(null, "用户名不能为空");
					}else if(userpassword.equals("")){
						JOptionPane.showMessageDialog(null, "密码不能为空");
						}
				}else{
						UserDo userlogin=new UserDo(textField.getText(),userpassword);
						Massage msg=new Massage(0,"","check",userlogin);
						tcp.sendOptmessage(msg);
						result=tcp.getResult();
						massage=tcp.getmsg();
						if(result.equals("true")){
							JOptionPane.showMessageDialog(null,massage);
							UserStore frame = new UserStore(jframe,textField.getText(),tcp);
							frame.setVisible(true);
							jframe.setVisible(false);
						}else{
							JOptionPane.showMessageDialog(null,massage);
							textField.setText(null);
							passwordField.setText(null);
						}
				}
			}
		});
		contentPane.add(button);
		
		JButton button2 = new JButton("用户注册");
		button2.setBounds(155, 216, 93, 23);
		button2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				UserAdd useradd=new UserAdd(tcp);
				useradd.setVisible(true);
			}
		});
		contentPane.add(button2);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(UserLogin.class.getResource("../images/a.jpg")));
		lblNewLabel.setBounds(208, 54, 86, 82);
		contentPane.add(lblNewLabel);
	}
}
